#include "SmoothingGroupSupport.h"
#include <map>
using namespace std;
IndexTranslator::IndexTranslator()
{
}

IndexTranslator::~IndexTranslator()
{
}

void IndexTranslator::AddVertexIndex(VertexIndexSupport& vis,int meshVertexIndex){
	m_translationMap[vis]=meshVertexIndex;
}

bool IndexTranslator::ContainsAbsVertexIndex(VertexIndexSupport vis){
	
	VISHASH::const_iterator it = m_translationMap.find(vis);
	return it != m_translationMap.end();
}

int IndexTranslator::GetMeshIndex(VertexIndexSupport vis){
	VISHASH::const_iterator it = m_translationMap.find(vis);
	 if( it == m_translationMap.end() )
		 return -1;
	 else
		 return it->second;
}

bool IndexTranslator::GetMeshIndex(VertexIndexSupport& index, int* meshIndex)
{
	VISHASH::const_iterator it = m_translationMap.find(index);
	if( it == m_translationMap.end() ){
		*meshIndex = -1;	
		return false;
	}else
		*meshIndex = it->second;
		return true;
}

SmoothingGroup::SmoothingGroup(void)
{
}

SmoothingGroup::~SmoothingGroup(void)
{
	map<int,IndexTranslator*>::const_iterator it;
	for (it = m_SmoothingGroupMap.begin();
		it != m_SmoothingGroupMap.end();
		++it){
		delete (*it).second;
	}
	m_SmoothingGroupMap.clear();
}

void SmoothingGroup::AddVertexIndex(int smoothingGroup, VertexIndexSupport& vis, int meshVertexIndex){
	map<int,IndexTranslator*>::const_iterator it = 
		m_SmoothingGroupMap.find(smoothingGroup);
	IndexTranslator* itrans = NULL;
	if ( it == m_SmoothingGroupMap.end() ){
		itrans = new IndexTranslator();
		m_SmoothingGroupMap[smoothingGroup]=itrans;
	}else{
		itrans = it->second;
	}
	itrans->AddVertexIndex(vis,meshVertexIndex);
}

bool SmoothingGroup::ContainsAbsVertexIndex(int smoothingGroup, VertexIndexSupport vis){
	map<int,IndexTranslator*>::const_iterator it = 
		m_SmoothingGroupMap.find(smoothingGroup);
	IndexTranslator* itrans = NULL;
	if ( it != m_SmoothingGroupMap.end() ){
		itrans = it->second;
		return itrans->ContainsAbsVertexIndex(vis);
	}else
		return false;
}

int SmoothingGroup::GetMeshIndex(int smoothingGroup, VertexIndexSupport& vis){
	map<int,IndexTranslator*>::const_iterator it = 
		m_SmoothingGroupMap.find(smoothingGroup);
	IndexTranslator* itrans = NULL;
	if ( it != m_SmoothingGroupMap.end() ){
		itrans = it->second;
		return itrans->GetMeshIndex(vis);
	}else
		return -1;
}

bool SmoothingGroup::GetMeshIndex(int smoothingGroup, VertexIndexSupport& vis, int* meshIndex)
{
	map<int,IndexTranslator*>::const_iterator it = 
		m_SmoothingGroupMap.find(smoothingGroup);
	IndexTranslator* itrans = NULL;
	if ( it != m_SmoothingGroupMap.end() ){
		itrans = it->second;
		return itrans->GetMeshIndex(vis,meshIndex);
	}else
		return false;
}